# Location of the Lua install directory
LUA=C:\Utils\Lua

# The default build statically links Lua. This is less flexible in that it is
# not possible to "require" dynamically loaded C extensions. However:
#   1. Incompatibly linked extensions can crash the host process - ie, TCC
#   2. The default load path for DLLs is relative to the hosting EXE rather
#      than the Lua DLL, making this less useful than it seems at first.
# The dynamically linked version is still built, as Luad.dll.
all: tcLua.dll tcLuad.dll

# Plugin linked to the Lua DLL
tcLuad.dll: Lua.o TakeCmd.lib
	gcc -o tcLuad.dll -shared -s Lua.o TakeCmd.lib $(LUA)\bin\lua51.dll -Wl,--add-stdcall-alias

# Plugin with Lua linked statically
tcLua.dll: Lua.o TakeCmd.lib
	gcc -o tcLua.dll -shared -s Lua.o TakeCmd.lib -L $(LUA)\lib -llua -Wl,--add-stdcall-alias

Lua.o: Lua.c
	gcc -c -Wall -O2 Lua.c -I sdk -I $(LUA)\include

# The following command file builds TakeCmd.lib, an import library suitable
# for linking to TakeCmd.dll with mingw (gcc). It is needed because TakeCmd.dll
# exports its symbols as bare names (Set_Cmd) where compiled C expects stdcall
# functions to be decorated with the number of bytes passed to the function
# (Set_Cmd@4). The TakeCmd.def file was generated by hand, using TakeCmd.h as
# a basis.

# The --kill-at flag is needed to cause the resulting import library to
# refer to the undecorated name in the DLL.
TakeCmd.lib: TakeCmd.def
	dlltool --kill-at --dllname TakeCmd.dll --def TakeCmd.def --output-lib TakeCmd.lib

clean:
	del TakeCmd.lib
	del Lua.o
	del tcLua.dll
	del tcLuad.dll
